package com.abctime.demo;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.abctime.api.ABCLibraryApi;
import com.abctime.api.PaymentJump;
import com.abctime.api.commonapi.IApiCallback;

/**
 * 应用内供sdk调用的支付界面
 */
public class PaymentActivity extends AppCompatActivity {

    private String uid;//该平台用户Id
    private String levelId;//用户选择当前level的Id
    private String levelName;//用户选择当前level的名字
    private String order_no;//支付成功拿到订单号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);

        getLibraryMessage();

        TextView view = findViewById(R.id.tv_message);
        view.setText(String.format("支付界面  uid = %s  levelId = %s  levelName = %s", uid, levelId, levelName));

        //这里随机生成，调试时需要替换成用户当前操作的订单Id
        order_no = "TAL" + System.currentTimeMillis() + levelId;

        createOrder();

        cancelOrder();
    }

    /**
     * 获取用户操作书架对应level的信息
     */
    private void getLibraryMessage() {
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        levelId = intent.getStringExtra("levelId");
        levelName = intent.getStringExtra("levelName");
    }

    private void createOrder() {
        TextView addOrder = findViewById(R.id.tv_add_order);
        addOrder.setText(String.format("提交订单 ：%s", order_no));
        addOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ABCLibraryApi.getApiService().addOrder(
                        uid, //该平台用户ID
                        levelId,//购买level对应id  /  全level购买默认Id 1000
                        levelName,//购买level对应name  /  全level购买默认name all
                        "1",//购买时长 1半年 2一年 3一月
                        "100",//购买支付金额
                        order_no,//第三方订单号
                        String.valueOf(System.currentTimeMillis() / 1000),//支付时间，精确到秒
                        new IApiCallback() {
                            @Override
                            public void onApiSuccess() {
                                //如果操作成功，需要设置返回成功标志，否则无法刷新书架绘本带锁状态
                                setResult(PaymentJump.RESULT_SUCCESS);

                                Toast.makeText(PaymentActivity.this, "订单添加成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onApiFail(String errCode, String errMsg) {
                                Toast.makeText(PaymentActivity.this, errMsg, Toast.LENGTH_SHORT).show();
                            }

                        }
                );
            }
        });
    }

    private void cancelOrder() {
        TextView cancelOrder = findViewById(R.id.tv_cancel_order);
        cancelOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ABCLibraryApi.getApiService().cancelOrder(
                        uid, //该平台用户ID
                        order_no,//第三方订单号
                        new IApiCallback() {
                            @Override
                            public void onApiSuccess() {
                                //如果操作成功，需要设置返回成功标志，否则无法刷新书架绘本带锁状态
                                setResult(PaymentJump.RESULT_SUCCESS);

                                Toast.makeText(PaymentActivity.this, "订单取消成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onApiFail(String errCode, String errMsg) {
                                Toast.makeText(PaymentActivity.this, errMsg, Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
    }

    public void finishView(View view) {
        finish();
    }
}
